package com.geping.etl.utils.common.commonsql;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wangzd
 * @Date: 16:46 2020/6/17
 */
public class ParamUtil{
    private String field;
    private String connector;
    private Object value;

    public ParamUtil() {
    }

    /**
     * @param field 字段名
     * @param matchType 连接符
     * @param value 字段值
     */
    public ParamUtil(String field, MatchType matchType, Object value) {
        this.field = field;
        this.connector = matchType.getConnector();
        this.value = value;
    }

    /**
     * 拼接条件
     * @return
     */
    public String where(){
        StringBuffer where=new StringBuffer();
        if ("sort".equals(getConnector())){
            where.append(" order by "+getField()+" "+getValue());
        }else {
            where.append(" and "+getField()+" "+getConnector()+" :"+getField());
        }
        return String.valueOf(where);
    }

    /**
     * 条件中有两个相同字段
     * @param count
     * @return
     */
    public String where(int count){
        StringBuffer where=new StringBuffer();
        where.append(" and "+getField()+" "+getConnector()+" :"+getField()+count);
        return String.valueOf(where);
    }

    public String or(ParamUtil paramUtil){
        StringBuffer where =new StringBuffer();
        where.append(" and ("+getField()+" "+getConnector()+" :"+getField());
        where.append(" or "+paramUtil.getField()+" "+getConnector()+" :"+getField()+") ");
        return String.valueOf(where);
    }




    public Map<String,Object> getParams(){
        Map<String,Object> map=new HashMap<>();
        map.put(getField(),getValue());
        return map;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getConnector() {
        return connector;
    }

    public void setConnector(String connector) {
        this.connector = connector;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }
}
